package com.wesley.redis;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author:wesley
 */
public class LockService {

    private static JedisPool conn = null;

    static {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(200);

        config.setMaxIdle(8);

        config.setMaxWaitMillis(1000*100);

        config.setTestOnBorrow(true);

        conn = new JedisPool(config, "127.0.0.1",6379,3000);
    }


    RedisDistributeLock lock = new RedisDistributeLock(conn);


    int n = 500;

    public void secKill(){
        String id = lock.lockWithTimeout("resource",5000,1000);
        System.out.println(Thread.currentThread().getName()+"获得了锁");
        System.out.println(--n);
        lock.releaseLock("resource",id);
    }

}
